package com.person.jiaiplan.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.jiaiplan.mapper.AccountMemberInfoMapper;
import com.person.jiaiplan.pojo.dto.expenses.AccountMemberAddReqDTO;
import com.person.jiaiplan.pojo.dto.expenses.AccountMemberInfoResDTO;
import com.person.jiaiplan.pojo.dto.expenses.AccountMemberQueryReqDTO;
import com.person.jiaiplan.pojo.entity.AccountMemberInfo;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.AccountMemberInfoService;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class AccountMemberInfoServiceImpl extends ServiceImpl<AccountMemberInfoMapper, AccountMemberInfo> implements AccountMemberInfoService {

    @Override
    public Boolean addAccountInfo(AccountMemberAddReqDTO reqDTO) {
        AccountMemberInfo accountMemberInfo = null;
        // 一个accountInfo 只能关联一个账号，否则就是修改
        if (StrUtil.isNotBlank(reqDTO.getCountId())) {
            accountMemberInfo = this.getOne(Wrappers.<AccountMemberInfo>lambdaQuery().eq(AccountMemberInfo::getCountId, Long.valueOf(reqDTO.getCountId())));
        }
        if (StrUtil.isNotBlank(reqDTO.getId())) {
            accountMemberInfo = this.getOne(Wrappers.<AccountMemberInfo>lambdaQuery().eq(AccountMemberInfo::getId, Long.valueOf(reqDTO.getId())));
        }

        if (accountMemberInfo == null) {
            accountMemberInfo = new AccountMemberInfo();
            accountMemberInfo.setId(SignleSnowFlake.getSnowflakeId());
            if (StrUtil.isNotBlank(reqDTO.getCountId())) {
                accountMemberInfo.setCountId(Long.valueOf(reqDTO.getCountId()));
            }

            accountMemberInfo.setAccountNo(reqDTO.getAccountNo());
            accountMemberInfo.setAccountType(reqDTO.getAccountType());
            accountMemberInfo.setContent(reqDTO.getContent());
            accountMemberInfo.setPassword(reqDTO.getPassword());
            accountMemberInfo.setRemark(reqDTO.getRemark());
            if (StrUtil.isNotBlank(reqDTO.getStartTime())) {
                accountMemberInfo.setStartTime(DateUtil.parseDate(reqDTO.getStartTime()));
            }
            if (StrUtil.isNotBlank(reqDTO.getEndTime())) {
                accountMemberInfo.setEndTime(DateUtil.parseDate(reqDTO.getEndTime()));
            }
            accountMemberInfo.setCreateTime(DateUtil.date());
            accountMemberInfo.setAddress(reqDTO.getAddress());
            return this.save(accountMemberInfo);
        } else {
            accountMemberInfo.setAccountNo(reqDTO.getAccountNo());
            accountMemberInfo.setAccountType(reqDTO.getAccountType());
            accountMemberInfo.setContent(reqDTO.getContent());
            accountMemberInfo.setPassword(reqDTO.getPassword());
            accountMemberInfo.setRemark(reqDTO.getRemark());
            if (StrUtil.isNotBlank(reqDTO.getStartTime())) {
                accountMemberInfo.setStartTime(DateUtil.parseDate(reqDTO.getStartTime()));
            }
            if (StrUtil.isNotBlank(reqDTO.getEndTime())) {
                accountMemberInfo.setEndTime(DateUtil.parseDate(reqDTO.getEndTime()));
            }
            accountMemberInfo.setUpdateTime(DateUtil.date());
            accountMemberInfo.setAddress(reqDTO.getAddress());
            return this.updateById(accountMemberInfo);
        }

    }

    @Override
    public List<AccountMemberInfoResDTO> queryList(AccountMemberQueryReqDTO reqDTO) {
        // 默认查询本月的数据
        if (StrUtil.isBlank(reqDTO.getStartTime()) || StrUtil.isBlank(reqDTO.getEndTime())) {
            reqDTO.setStartTime(DateUtil.format(DateUtil.beginOfMonth(DateUtil.date()), "yyyy-MM-dd"));
            reqDTO.setEndTime(DateUtil.format(DateUtil.endOfMonth(DateUtil.date()), "yyyy-MM-dd"));
        }
        LambdaQueryWrapper<AccountMemberInfo> queryWrapper = Wrappers.<AccountMemberInfo>lambdaQuery();
        queryWrapper.le(AccountMemberInfo::getStartTime, DateUtil.beginOfDay(DateUtil.parseDate(reqDTO.getEndTime())));
        queryWrapper.ge(AccountMemberInfo::getEndTime, DateUtil.endOfDay(DateUtil.parseDate(reqDTO.getStartTime())));

        if (reqDTO.getAccountType() != null) {
            queryWrapper.eq(AccountMemberInfo::getAccountType, reqDTO.getAccountType());
        }
        List<AccountMemberInfo> lists = this.list(queryWrapper);
        // 加上没有时间限制的会员号
        queryWrapper = Wrappers.<AccountMemberInfo>lambdaQuery();
        if (reqDTO.getAccountType() != null) {
            queryWrapper.eq(AccountMemberInfo::getAccountType, reqDTO.getAccountType());
        }
        queryWrapper.isNull(AccountMemberInfo::getStartTime);
        queryWrapper.isNull(AccountMemberInfo::getEndTime);
        lists.addAll(this.list(queryWrapper));

        return createResDTO(lists);
    }

    /**
     * 创建查询返回对象
     *
     * @param lists
     * @return
     */
    private List<AccountMemberInfoResDTO> createResDTO(List<AccountMemberInfo> lists) {
        List<AccountMemberInfoResDTO> resDTOS = new LinkedList<>();
        lists.stream().forEach(vo -> {
            AccountMemberInfoResDTO accountMemberInfoResDTO = new AccountMemberInfoResDTO();
            accountMemberInfoResDTO.setId(String.valueOf(vo.getId()));
            accountMemberInfoResDTO.setCountId(String.valueOf(vo.getCountId()));
            accountMemberInfoResDTO.setAccountType(vo.getAccountType());
            accountMemberInfoResDTO.setContent(vo.getContent());
            accountMemberInfoResDTO.setStartTime(DateUtil.format(vo.getStartTime(), "yyyy-MM-dd"));
            accountMemberInfoResDTO.setEndTime(DateUtil.format(vo.getEndTime(), "yyyy-MM-dd"));
            accountMemberInfoResDTO.setAccountNo(vo.getAccountNo());
            accountMemberInfoResDTO.setPassword(vo.getPassword());
            accountMemberInfoResDTO.setRemark(vo.getRemark());
            if (vo.getStartTime() == null || vo.getEndTime() == null) {
                accountMemberInfoResDTO.setValidTime("永久");
            } else {
                String validTime = DateUtil.format(vo.getStartTime(), "yyyy-MM-dd") + "-" + DateUtil.format(vo.getEndTime(), "yyyy-MM-dd");
                accountMemberInfoResDTO.setValidTime(validTime);
            }

            accountMemberInfoResDTO.setAddress(vo.getAddress());
            resDTOS.add(accountMemberInfoResDTO);
        });
        return resDTOS;
    }
}
